package com.hy.app;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.hy.app.camera.CameraGLActivity;
import com.hy.app.camera.CameraTextureActivity;
import com.hy.app.cube.CubeActivity;
import com.hy.app.databinding.ActivityMainBinding;
import com.hy.app.pic.PictureActivity;
import com.hy.app.point.PointActivity;
import com.hy.app.rectangle.RectangleActivity;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
    private static final String TAG = "MainActivity";

    private final String[] permissions = {Manifest.permission.CAMERA};

    private ActivityMainBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());

        initView();
    }

    private void initView() {
        mBinding.drawBaseColorBtn.setOnClickListener(view -> {
            startActivity(new Intent(MainActivity.this, BaseColorActivity.class));
        });

        mBinding.drawTriangleBtn.setOnClickListener(view -> {
            startActivity(new Intent(MainActivity.this, TriangleActivity.class));
        });

        mBinding.drawRectangleBtn.setOnClickListener(view -> {
            startActivity(new Intent(MainActivity.this, RectangleActivity.class));
        });

        mBinding.drawPointBtn.setOnClickListener(view -> {
            startActivity(new Intent(MainActivity.this, PointActivity.class));
        });

        mBinding.drawCubeBtn.setOnClickListener(view -> {
            startActivity(new Intent(MainActivity.this, CubeActivity.class));
        });

        mBinding.drawBitmapBtn.setOnClickListener(view -> {
            startActivity(new Intent(MainActivity.this, PictureActivity.class));
        });

        mBinding.cameraPreviewBtn.setOnClickListener(v -> {
            if (EasyPermissions.hasPermissions(MainActivity.this, permissions)) {
                startActivity(new Intent(MainActivity.this, CameraGLActivity.class));
            } else {
                EasyPermissions.requestPermissions(this, "该功能需要申请相机权限", 0x101, permissions);
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (requestCode == 0x101) {
            Log.d(TAG, "相机权限已申请");
            startActivity(new Intent(MainActivity.this, CameraTextureActivity.class));
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {

    }
}